#include "KDebug.h"
#include "K_Windows.h"
#include "KAutoPtr.h"
#include <cstdio>
#include <string>

int kTrace(bool addCrLf, const char *format, ...)
{
    assert(format != NULL);
    std::string fmt;
    if (addCrLf) {
        fmt = format;
        fmt.append("\r\n");
        format = fmt.c_str();
    }
    va_list args;
    va_start(args, format);
    int cch = vsnprintf(NULL, 0, format, args);
    if (cch > 0) {
        KAutoPtr<char, true> buf(new char[cch + 1]);
        if ((cch = vsprintf(buf, format, args)) > 0) {
            OutputDebugStringA(buf);
        }
    }
    va_end(args);
    return cch;
}